// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.1
import "Variable.js" as Variable
Page{
    id: home_page
    onHeightChanged: {
        hearder.height = parent.width < parent.height ? 72 : 50
    }
    tools: ToolBarLayout {
        id: commonTools
        visible: true
        ToolIcon {
            platformIconId: "toolbar-view-menu"
            anchors.right: (parent === undefined) ? undefined : parent.right
            onClicked: (myMenu.status === DialogStatus.Closed) ? myMenu.open() : myMenu.close()
        }
        ToolButton{
            text: "Login"
            onClicked: login.open();
        }

        ToolIcon {
            platformIconId: "toolbar-view-menu"
            anchors.right: (parent === undefined) ? undefined : parent.right
//            onClicked: appWindow.pageStack.push(Qt.resolvedUrl("Settings.qml"));
            onClicked: aboutDialog.open();
        }
    }
    Header{
        id: hearder
        height: 72
        z:1
        anchors.left: parent.left
        anchors.leftMargin: 0
        anchors.right: parent.right
        anchors.rightMargin: 0
        title: "QVDM"
        Behavior on height { NumberAnimation { duration: 200 }}
    }
    QueryDialog {
        id: aboutDialog
        titleText: "QVDM"
        message: "Yavuz EKMEN\nVersion: 0.2.4 \nMail: y.ekmen@ovi.com\n"
    }
    InfoBanner {
        id: infobanner
        z:2
    }
    function showBanner(text) {
        infobanner.text = text
        infobanner.timerShowTime = 1000;
        infobanner.show();
    }
    function activeBtn(){
        button_amour.enabled = true;
        button_animaux.enabled = true;
        button_argent.enabled = true;
        button_enfants.enabled = true;
        button_flop.enabled = true;
        button_inclassable.enabled = true;
        button_last.enabled = true;
        button_sante.enabled = true;
        button_top.enabled = true;
        button_travail.enabled = true;
        button_user.enabled = Variable.loginOk;
    }
    function desactiveBtn()
    {
        button_amour.enabled = false;
        button_animaux.enabled = false;
        button_argent.enabled = false;
        button_enfants.enabled = false;
        button_flop.enabled = false;
        button_inclassable.enabled = false;
        button_last.enabled = false;
        button_sante.enabled = false;
        button_top.enabled = false;
        button_travail.enabled = false;
        button_user.enabled = false;
    }
    function reloadLogin()
    {
        xmlModel_login.xml = Variable.xmlDataLogin;
        xmlModel_login.reload();
    }
    function load(link)
    {
        console.debug("Dans Load")
        flickable.state = "loading"
        hearder.loading();
        desactiveBtn();
        var doc = new XMLHttpRequest();
        doc.onreadystatechange = function()
                {
                    if (doc.readyState === XMLHttpRequest.DONE)
                    {
                        Variable.xmlData = doc.responseText
//                        Variable.nb = doc.responseText
                            appWindow.pageStack.push(Qt.resolvedUrl("MainPage.qml"));

                        hearder.finish();
                        flickable.state = "finish"
                        activeBtn();
                    }
                }
        doc.open("GET", link);
        doc.send();
    }
    function loginRequest(user, pass)
    {
        var doc = new XMLHttpRequest();
        var link = "http://api.fmylife.com/account/login/"+user+"/"+pass+"?key=4fd9d0cb0463c&language=fr"
        console.debug("LoginRequest : " + link)
        doc.onreadystatechange = function()
                {
                    if (doc.readyState == XMLHttpRequest.DONE)
                    {
                        Variable.xmlDataLogin = doc.responseText
                        reloadLogin();
                    }
                }
        doc.open("POST", link);
        doc.send();
    }
    Flickable{
        id: flickable
        z:0
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 0
        anchors.left: parent.left
        anchors.leftMargin: 0
        anchors.right: parent.right
        anchors.rightMargin: 0
        anchors.top: hearder.bottom
        anchors.topMargin: 0
        contentHeight: sousTitre.width * 2
        state: "finish"
        Label{
            id: titre
            width: 300
            height: 50
            text: "Vie de merde"
            font.pixelSize: 35
            horizontalAlignment: Text.AlignHCenter
            font.bold: true
            anchors.horizontalCenter: parent.horizontalCenter
        }
        Label{
            id: sousTitre
            width: 400
            height: 20
            text: "Ma vie c'est de la merde, et je vous emmerde"
            font.pixelSize: 18
            opacity: 0.500
            wrapMode: Text.WrapAtWordBoundaryOrAnywhere
            horizontalAlignment: Text.AlignHCenter
            anchors.top: titre.bottom
            anchors.topMargin: 10
            anchors.horizontalCenter: parent.horizontalCenter

        }

        Button{
            id: button_amour
            text: "Amour"
            height: 50
            anchors.top: sousTitre.bottom
            anchors.topMargin: 30
            anchors.left: parent.left
            anchors.leftMargin: 50
            anchors.right: parent.right
            anchors.rightMargin: 50
            onClicked: {
                Variable.setCategorie(1);
                load("http://api.fmylife.com/view/amour?key=4fd9d0cb0463c&language=fr");
            }
        }
        Button{
            id: button_animaux
            text: "Animaux"
            anchors.top: button_amour.bottom
            height: 50
            anchors.topMargin: 10
            anchors.left: parent.left
            anchors.leftMargin: 50
            anchors.right: parent.right
            anchors.rightMargin: 50
            onClicked: {
                Variable.setCategorie(2);
                load("http://api.fmylife.com/view/animaux?key=4fd9d0cb0463c&language=fr")
            }
        }
        Button{
            id: button_argent
            text: "Argent"
            height: 50
            anchors.top: button_animaux.bottom
            anchors.topMargin: 10
            anchors.left: parent.left
            anchors.leftMargin: 50
            anchors.right: parent.right
            anchors.rightMargin: 50
            onClicked: {
                Variable.setCategorie(3);
                load("http://api.fmylife.com/view/argent?key=4fd9d0cb0463c&language=fr")
            }
        }
        Button{
            id: button_enfants
            text: "Enfant"
            height: 50
            anchors.top: button_argent.bottom
            anchors.topMargin: 10
            anchors.left: parent.left
            anchors.leftMargin: 50
            anchors.right: parent.right
            anchors.rightMargin: 50
            onClicked: {
                Variable.setCategorie(4);
                load("http://api.fmylife.com/view/enfants?key=4fd9d0cb0463c&language=fr")
            }
        }
        Button{
            id: button_travail
            text: "Travail"
            height: 50
            anchors.top: button_enfants.bottom
            anchors.topMargin: 10
            anchors.left: parent.left
            anchors.leftMargin: 50
            anchors.right: parent.right
            anchors.rightMargin: 50
            onClicked: {
                Variable.setCategorie(5);
                load("http://api.fmylife.com/view/travail?key=4fd9d0cb0463c&language=fr")
            }
        }
        Button{
            id: button_sante
            text: "Sante"
            height: 50
            anchors.top: button_travail.bottom
            anchors.topMargin: 10
            anchors.left: parent.left
            anchors.leftMargin: 50
            anchors.right: parent.right
            anchors.rightMargin: 50
            onClicked: {
                Variable.setCategorie(6);
                load("http://api.fmylife.com/view/sante?key=4fd9d0cb0463c&language=fr")
            }
        }
//        Button{
//            id: button_sexe
//            text: "Sexe"
//            height: 50
//            anchors.top: button_sante.bottom
//            anchors.topMargin: 10
//            anchors.left: parent.left
//            anchors.leftMargin: 50
//            anchors.right: parent.right
//            anchors.rightMargin: 50
//            onClicked: {
//                Variable.setCategorie(7);
//                load("http://api.fmylife.com/view/sexe?key=4fd9d0cb0463c&language=fr")
//            }
//        }
        Button{
            id: button_inclassable
            text: "Inclassable"
            height: 50
            anchors.top: button_sante.bottom
            anchors.topMargin: 10
            anchors.left: parent.left
            anchors.leftMargin: 50
            anchors.right: parent.right
            anchors.rightMargin: 50
            onClicked: {
                Variable.setCategorie(8);
                load("http://api.fmylife.com/view/inclassable?key=4fd9d0cb0463c&language=fr")
            }
        }

        Button {
            id: button_top
            width: 100
            height: 50
            text: qsTr("Top")
            anchors.left: parent.left
            anchors.leftMargin: 15
            anchors.top: button_inclassable.bottom
            anchors.topMargin: 20
            onClicked: {
                Variable.setCategorie(11);
                load("http://api.fmylife.com/view/top?key=4fd9d0cb0463c&language=fr")
            }
        }

        Button {
            id: button_flop
            y: 701
            width: 100
            height: 50
            text: qsTr("Flop")
            anchors.left: button_top.right
            anchors.leftMargin: 20
            anchors.top: button_inclassable.bottom
            anchors.topMargin: 20
            onClicked: {
                Variable.setCategorie(10);
                load("http://api.fmylife.com/view/flop?key=4fd9d0cb0463c&language=fr")
            }
        }

//        Button {
//            id: button_favoris      //Seulement si identifier
//            y: 672
//            height: 50
//            text: qsTr("Favo.")
//            anchors.right: parent.right
//            anchors.rightMargin: 10
//            anchors.left: button_last.right
//            anchors.leftMargin: 20
//            anchors.top: button_inclassable.bottom
//            anchors.topMargin: 20
//            onClicked: {
//                load("http://sandbox.betacie.com/view/favorites?key=4fd9d0cb0463c&language=fr")
//            }
//        }

        Button {
            id: button_last
            y: 701
            height: 50
            text: qsTr("Nouv.")
            anchors.right: parent.right
            anchors.rightMargin: 5
            anchors.left: button_flop.right
            anchors.leftMargin: 20
            anchors.top: button_inclassable.bottom
            anchors.topMargin: 20
            onClicked: {
                Variable.setCategorie(9);
                load("http://api.fmylife.com/view/last?key=4fd9d0cb0463c&language=fr")
            }
        }
        Button {
            id: button_user
            height: 50
            anchors.top: button_top.bottom
            anchors.topMargin: 20
            anchors.left: parent.left
            anchors.leftMargin: 50
            anchors.right: parent.right
            anchors.rightMargin: 50
            text: "Mon espace"
            enabled: Variable.loginOk;
            onClicked: {
                Variable.lien = "http://api.fmylife.com/account/infos?login=beter93&key=4fd9d0cb0463c&language=fr"
                Variable.load_pages(pageStack, 1);
            }
        }

        states: [
            State {
                name: "loading"
                PropertyChanges {
                    target: flickable
                    opacity: 0.2
                    interactive: false
                }
            },
            State {
                name: "finish"
                PropertyChanges {
                    target: flickable
                    opacity: 1
                    interactive: true
                }
            }
        ]
        transitions: [
            Transition {
                from: "*"
                to: "*"
                NumberAnimation{
                    duration: 400
                }
            }
        ]
    }
    Login{
        id: login
        onAccepted: loginRequest(Variable.user,Variable.pass)
    }

    XmlListModel{
        id: xmlModel_login
        query: "/root"
        XmlRole { name: "login"; query: "login/string()"}
        XmlRole { name: "token"; query: "token/string()" }
        XmlRole { name: "activeKey"; query: "active_key/string()" }
        XmlRole { name: "errorCode"; query: "errors/error/@code/string()" }
        XmlRole { name: "error"; query: "errors/error/string()" }
        onXmlChanged: {
            xmlModel_login.errorString();
        }
        onProgressChanged: {
            hearder.value = progress
            console.debug("[Acceuil] Progression ... " + progress)
        }

//        2001: Invalid or inexistent token
//        2002: Already voted.
//        2003: Banned member
//        2004: Account is unverified
//        2005: Bad login information.
//        2006: This user name or email address is already in use on the website.
//        2007: Anti-flood protection
//        2008: Banned IP address
//        2009: Out of time limit
//        2010: Specified member doesn't exist
        onStatusChanged:{
           var code  = xmlModel_login.get(0).errorCode;
           var errString = xmlModel_login.get(0).error;
            console.debug("ErrorCode = "  + code)
            if(code === "")
            {
                login.close();
                console.debug("Authentification Reussi")
                login.msg = "Authentification Reussi"       //Changement du msg du Dialog Login
                showBanner("Authentification réussi");      //Affichage

                Variable.loginOk = true;        //Authentification OK, activation de la variable globale
                button_user.enabled = Variable.loginOk;     //Activation du boutton
            }
            else if(code === "2005"){
                console.debug("Utilisateur incorrect: " + errString)
                Variable.loginOk = false;
                login.msg = "Utilisateur incorrect"
                login.open()
//                infobanner.timerShowTime = 0;
                showBanner(errString);
            }
            else if(code === "2003"){
                console.debug("Utilisateur Banni: " + errString)
                login.msg = "Utilisateur Banni"
                login.open()
                showBanner(errString);
            }
            else if(code === "2006"){
                console.debug(errString)
                showBanner(errString);
            }
            else if(code === "2008"){
                console.debug("IP Banni" + errString)
                showBanner(errString);
            }
            else
            {
                console.debug("Error :" + code)
                showBanner(errString);
            }
        }
    }
}
